/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package spidex.resources;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;


/**
 *
 * @author xatrix
 */
public class UserDirManager {
    public static final UserDirManager MANAGER = new UserDirManager();
    public String sysName = System.getProperty("os.name");
    public ResourceBundle bundle = 
        ResourceBundle.getBundle("spidex/resources/properties");

    private UserDirManager() {
    }
    
    public static UserDirManager getInstance() {
        return MANAGER;
    }
    
    public Map<String,String> checkUserSettings() throws IOException {
        System.out.println("System: "+sysName);
        if(sysName.equals("Linux") ) {
            return createUnixSettings();
        }
        if(sysName.equals("Windows XP")) {
            return createWinSettings();
        }
        else{
            return createWinSettings();
        }
    }
    
    private Map<String,String> createUnixSettings() throws IOException {
        String homePath = "/home/"+System.getProperty("user.name")+"/.spidex/";
        String posterPath = homePath+"Posters/";

        File dir = new File(homePath);
        File pictureDir = new File(posterPath);

        if(!dir.exists() && !dir.mkdir() ) {
            throw new IOException("Can't create directory for user settings "+
                    homePath);
        }
        
        if(!pictureDir.exists() && !pictureDir.mkdir() ) {
            throw new IOException(
                "Can't create poster directory for user settings "
            );
        }
        //TODO Doplneni XML konfigurate
        Map<String,String> map = new HashMap<String,String>();
        map.put("homedir", homePath);
        map.put("posterdir", posterPath);
        map.put("database", homePath+"database");
        return map;
    }
    
    private Map<String,String> createWinSettings() throws  IOException {
        String homePath = "C:\\Program Files\\spidex\\";
        String homeUserPath = homePath+System.getProperty("user.name")+"\\";
        String posterPath = homeUserPath+"Posters\\";
        System.out.println(posterPath);
        File dir = new File(homePath);
        File userdir = new File(homeUserPath);
        File pictureDir = new File(posterPath);

        if(!dir.exists() && !dir.mkdir() || 
                !userdir.exists() && !userdir.mkdir() ) {
            JOptionPane.showMessageDialog(null, 
                bundle.getString("error_settings")
            );
            throw new IOException("Can't create directory for user settings "+
                    homeUserPath);
            
        }
        
        if(!pictureDir.exists() && !pictureDir.mkdir() ) {
            JOptionPane.showMessageDialog(null, 
                bundle.getString("error_poster")
            );
            throw new IOException(
                "Can't create poster directory for user settings "
            );
        }
        
        //TODO Doplneni XML konfigurate
        Map<String,String> map = new HashMap<String,String>();
        map.put("homedir", homePath);
        map.put("posterdir", posterPath);
        map.put("database", homePath+"database");
        return map;
    }
    
    public static void main(String args[]) throws IOException {
      UserDirManager.getInstance().checkUserSettings();
    }  
}
